package com.lingxu.api.ztb.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingxu.api.ztb.mapper.ProjectFeasibilityMapper;
import com.lingxu.api.ztb.model.ProjectFeasibility;
import com.lingxu.api.ztb.model.vo.ConstructionDrawingDesignVO;
import com.lingxu.api.ztb.model.vo.ProjectFeasibilityVO;
import com.lingxu.api.ztb.service.IProjectFeasibilityService;


import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;


import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 工程可行性研究基础信息表 服务实现类
 *
 * @author admin
 * @since 2025-08-12
 */
@Slf4j
@Service
public class ProjectFeasibilityServiceImpl extends ServiceImpl<ProjectFeasibilityMapper, ProjectFeasibility>  implements IProjectFeasibilityService {

    @Resource
    private ProjectFeasibilityMapper projectFeasibilityMapper;

    @Transactional(readOnly = true)
    @Override
    public IPage<ProjectFeasibilityVO> queryPageList(ProjectFeasibilityVO query) {
        Page<ProjectFeasibilityVO> page = new Page<ProjectFeasibilityVO>(query.getPageNo(), query.getPageSize());
        return projectFeasibilityMapper.pageList(page,query);
    }


    @Transactional(readOnly = true)
    @Override
    public List<ProjectFeasibilityVO> list(ProjectFeasibilityVO query) {
        return projectFeasibilityMapper.selectProjectFeasibilityList(query);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Set<String> ids) {
        if (CollectionUtils.isNotEmpty(ids)) {
            projectFeasibilityMapper.deleteBatchIds(ids);
        }
    }

    @Override
    public ProjectFeasibilityVO queryPageInfo(ProjectFeasibilityVO query) {
        List<ProjectFeasibilityVO> list = projectFeasibilityMapper.queryPageInfo(query.getProjectName());
        if (list == null || list.isEmpty()) {
            return null;
        }
        ProjectFeasibilityVO response = new ProjectFeasibilityVO();
        ProjectFeasibilityVO first = list.get(0);
        response.setProjectName(first.getProjectName());       // 项目名称
        response.setProjectCode(first.getProjectCode());       // 项目编号
        response.setApprovalDate(first.getApprovalDate());     // 申报批复时间

        Set<String> designDepartments = new HashSet<>();
        Set<String> designDeptHeads = new HashSet<>();
        Set<String> designUnits = new HashSet<>();
        Set<String> designHeads = new HashSet<>();
        Set<String> approvalUnits = new HashSet<>();

        for (ProjectFeasibilityVO project : list) {
            addIfNotNull(designDepartments, project.getApplyUnit());//申报单位
            addIfNotNull(designDeptHeads, project.getApplyLeader());//申报负责人
            addIfNotNull(designUnits, project.getFeasibilityUnit());//设计单位
            addIfNotNull(designHeads, project.getUnitContactPerson());//设计单位负责人
            addIfNotNull(approvalUnits, project.getApprovalUnit()); //审批单位
        }

        response.setApplyUnit(joinSet(designDepartments));
        response.setApplyLeader(joinSet(designDeptHeads));
        response.setFeasibilityUnit(joinSet(designUnits));
        response.setUnitContactPerson(joinSet(designHeads));
        response.setApprovalUnit(joinSet(approvalUnits));

        return response;
    }

    private void addIfNotNull(Set<String> set, String value) {
        if (value != null && !value.trim().isEmpty()) {
            set.add(value.trim());
        }
    }

    private String joinSet(Set<String> set) {
        if (set == null || set.isEmpty()) {
            return "";
        }
        return String.join("/", set);
    }


}
